/*
   Copyright (c) 2011, 2012, 2015 Mizar Tools Contributors (mizartools.org)

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
*/
/*  Contributors : 
 *  Marco Riccardi
 *	2011-03-05 - initial implementation
 *	2011-03-20 - added menu System/Environment
 *  2012-04-06 - changed MainFrame constructor 
 *	2012-04-14 - added Reduction Registrations
 *	2015-01-11 - added Property Registrations
 *  2015-02-07 - added menu Utility/Decoded Library Item
 *
 */
package org.mizartools.example.ui;

import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.beans.PropertyVetoException;

import javax.swing.JDesktopPane;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

import org.mizartools.system.EnvironFile;
import org.mizartools.system.executable.ExecutableFileManager;
import org.mizartools.utility.TemporaryDirectoryException;
import org.mizartools.utility.TemporaryDirectoryFactory;

public class MainFrame extends JFrame {

	private static final long serialVersionUID = 1L;
	private JDesktopPane desktop;
	private ArticlesFrame articlesFrame = null;
	private KeywordsFrame keywordsFrame = null;
	private MessagesFrame messagesFrame = null;
	private VocabulariesFrame vocabulariesFrame = null;
	private UtilityDliFrame utilityDliFrame = null;
	private TestFrame testFrame = null;
	
    private final int inset = 50;
	private String aboutMessage = "org.mizartools.example version <<version>>\n\n<<copyright>>\n\nContributors:\nMarco Riccardi";

		public MainFrame(String version, String copyright){
        super("org.mizartools.example");
        aboutMessage = aboutMessage.replace("<<version>>", version);
        aboutMessage = aboutMessage.replace("<<copyright>>", copyright);
        try {UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());} 
        catch (ClassNotFoundException e1) {} 
        catch (InstantiationException e1) {} 
        catch (IllegalAccessException e1) {} 
        catch (UnsupportedLookAndFeelException e1) {}
        
		this.setTitle("org.mizartools.example");
        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
        setBounds(inset, inset, screenSize.width - inset*2, screenSize.height - inset*2 );
    	addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e){quit();}
    	});
        buildMenu();
		buildContent();
	}

    private void buildMenu(){
		JMenuBar menuBar = new JMenuBar();
		menuBar.setOpaque(true);
		//Library
		JMenu menuLibrary = new JMenu("Library");
		//Library/Articles
		JMenuItem menuItemArticles = new JMenuItem("Articles");
		menuItemArticles.addActionListener(new ActionListener() {
		    public void actionPerformed(ActionEvent e) {showArticles();}
		});
		menuLibrary.add(menuItemArticles);

		//Library/Vocabularies
		JMenuItem menuItemVocabularies = new JMenuItem("Vocabularies");
		menuItemVocabularies.addActionListener(new ActionListener() {
		    public void actionPerformed(ActionEvent e) {showVocabularies();}
		});
		menuLibrary.add(menuItemVocabularies);
		menuBar.add(menuLibrary);
		
		//Utility
		JMenu menuUtility = new JMenu("Utility");
		//Utility/Decoded Library Item
		JMenuItem menuItemDli = new JMenuItem("Decoded Library Item");
		menuItemDli.addActionListener(new ActionListener() {
		    public void actionPerformed(ActionEvent e) {showDli();}
		});
		menuUtility.add(menuItemDli);
		menuBar.add(menuUtility);
				
		//System
		JMenu menuSystem = new JMenu("System");
		//System/Environment 
		JMenuItem menuItemEnvironment = new JMenuItem("Environment");
		menuItemEnvironment.addActionListener(new ActionListener() {
		    public void actionPerformed(ActionEvent e) {showEnvironment();}
		});
		menuSystem.add(menuItemEnvironment);
		//System/Messages
		JMenuItem menuItemMessages = new JMenuItem("Messages");
		menuItemMessages.addActionListener(new ActionListener() {
		    public void actionPerformed(ActionEvent e) {showMessages();}
		});
		menuSystem.add(menuItemMessages);
		//System/Keyword
		JMenuItem menuItemKeywords = new JMenuItem("Keywords");
		menuItemKeywords.addActionListener(new ActionListener() {
		    public void actionPerformed(ActionEvent e) {showKeywords();}
		});
		menuSystem.add(menuItemKeywords);
		//System/Version
		JMenuItem menuItemVersion = new JMenuItem("Version");
		menuItemVersion.addActionListener(new ActionListener() {
		    public void actionPerformed(ActionEvent e) {showVersion();}
		});
		menuSystem.add(menuItemVersion);
		menuBar.add(menuSystem);

		//Help
		JMenu menuHelp = new JMenu("Help");
		//Help/Test
		//menuAbout.setAccelerator(KeyStroke.getAWTKeyStroke(KeyChar, modifiers));
		JMenuItem menuItemTest = new JMenuItem("Test");
		menuItemTest.addActionListener(new ActionListener() {
		    public void actionPerformed(ActionEvent e) {showTest();}
		});
		menuHelp.add(menuItemTest);
		//Help/About
		JMenuItem menuItemAbout = new JMenuItem("About");
		menuItemAbout.addActionListener(new ActionListener() {
		    public void actionPerformed(ActionEvent e) {showAboutBox();}
		});
		menuHelp.add(menuItemAbout);
		menuBar.add(menuHelp);

		setJMenuBar(menuBar);
    }
    
    private void buildContent() {
        desktop = new JDesktopPane();
        getContentPane().add(desktop);
    }

    private void quit() {
        System.exit(0);
    }
    
    private void showAboutBox() {
        JOptionPane.showMessageDialog(this, aboutMessage);
    }
    
    private void showEnvironment(){
    	StringBuilder version = new StringBuilder("");
    	version.append("MIZFILES : " + 
    				EnvironFile.getMIZFILES()+ "\n");
    	version.append("Binary Path : " + ExecutableFileManager.getInstance().getBinaryRoot()+"\n\n");
    	try {
			version.append("Working Directory : " + TemporaryDirectoryFactory.getRootTemporaryDirectory()+"\n");
		} catch (TemporaryDirectoryException e) {
			version.append("Working Directory : " + "null" +"\n");
		}
        JOptionPane.showMessageDialog(this,version);
    }

    private void showVersion(){
    	StringBuilder version = new StringBuilder("");
    	version.append("Version of Mizar System : " + 
    				EnvironFile.getMizarReleaseNbr()+ "." + 
    				EnvironFile.getMizarVersionNbr()+ "." +
    				EnvironFile.getMizarVariantNbr()+"\n");
    	version.append("Number of articles : " + EnvironFile.getNumberOfArticles()+"\n");
    	version.append("Version of Mizar Mathematical Library : " + EnvironFile.getMMLVersion()+"\n");
        JOptionPane.showMessageDialog(this,version);
    }
    
    private void showKeywords(){
		keywordsFrame = new KeywordsFrame();
		new Thread(keywordsFrame).start();
    	desktop.add(keywordsFrame);
   		keywordsFrame.setVisible(true);
	}

    private void showMessages(){
		messagesFrame = new MessagesFrame();
		new Thread(messagesFrame).start();
    	desktop.add(messagesFrame);
   		messagesFrame.setVisible(true);
	}

    private void showArticles(){
		articlesFrame = new ArticlesFrame(this);
		new Thread(articlesFrame).start();
    	desktop.add(articlesFrame);
    	articlesFrame.setVisible(true);
    	try {articlesFrame.setMaximum(true);
		} catch (PropertyVetoException e) {}
	}

    private void showVocabularies(){
		vocabulariesFrame = new VocabulariesFrame();
		new Thread(vocabulariesFrame).start();
    	desktop.add(vocabulariesFrame);
    	vocabulariesFrame.setVisible(true);
    	try {vocabulariesFrame.setMaximum(true);
		} catch (PropertyVetoException e) {}
	}

    private void showDli(){
		utilityDliFrame = new UtilityDliFrame();
    	desktop.add(utilityDliFrame);
    	utilityDliFrame.setVisible(true);
    	try {utilityDliFrame.setMaximum(true);
		} catch (PropertyVetoException e) {}
	}
    
    private void showTest(){
		testFrame = new TestFrame();
    	desktop.add(testFrame);
    	testFrame.setVisible(true);
    	try {testFrame.setMaximum(true);
		} catch (PropertyVetoException e) {}
	}
    
    public void showConstructor(String aid){
    	ConstructorFrame constructorFrame = new ConstructorFrame(aid);
    	new Thread(constructorFrame).start();
    	desktop.add(constructorFrame);
    	constructorFrame.setVisible(true);
    }

	public void showNotation(String aid) {
    	NotationFrame notationFrame = new NotationFrame(aid);
    	new Thread(notationFrame).start();
    	desktop.add(notationFrame);
    	notationFrame.setVisible(true);
	}

	public void showDefinientia(String aid) {
    	DefinientiaFrame definientiaFrame = new DefinientiaFrame(aid);
    	new Thread(definientiaFrame).start();
    	desktop.add(definientiaFrame);
    	definientiaFrame.setVisible(true);
	}

	public void showRegistration(String aid) {
		RegistrationFrame registrationFrame = new RegistrationFrame(aid);
    	new Thread(registrationFrame).start();
    	desktop.add(registrationFrame);
    	registrationFrame.setVisible(true);
	}

	public void showIdentifyRegistration(String aid) {
		IdentifyRegistrationFrame identifyRegistrationFrame = new IdentifyRegistrationFrame(aid);
    	new Thread(identifyRegistrationFrame).start();
    	desktop.add(identifyRegistrationFrame);
    	identifyRegistrationFrame.setVisible(true);
	}

	public void showPropertyRegistration(String aid) {
		PropertyRegistrationFrame propertyRegistrationFrame = new PropertyRegistrationFrame(aid);
    	new Thread(propertyRegistrationFrame).start();
    	desktop.add(propertyRegistrationFrame);
    	propertyRegistrationFrame.setVisible(true);
	}
	
	public void showReductionRegistration(String aid) {
		ReductionRegistrationFrame reductionRegistrationFrame = new ReductionRegistrationFrame(aid);
    	new Thread(reductionRegistrationFrame).start();
    	desktop.add(reductionRegistrationFrame);
    	reductionRegistrationFrame.setVisible(true);
	}

	public void showTheorem(String aid) {
		TheoremFrame theoremFrame = new TheoremFrame(aid);
    	new Thread(theoremFrame).start();
    	desktop.add(theoremFrame);
    	theoremFrame.setVisible(true);
	}

	public void showScheme(String aid) {
		SchemeFrame schemeFrame = new SchemeFrame(aid);
    	new Thread(schemeFrame).start();
    	desktop.add(schemeFrame);
    	schemeFrame.setVisible(true);
	}
    
}
